package com.jdsh.encrypt.utils;


import com.alibaba.fastjson.JSONObject;
import org.bouncycastle.math.ec.ECPoint;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;


public class Sm2Util {
    //private final Log logger = LogFactory.getLog(this.getClass());

    public static String encrypted(String data) throws UnsupportedEncodingException {
        SM2 s2 = new SM2();
        BigInteger privateKey = SM2.importPrivateKey("D:/Certificates/privatekeyB.pem");
        ECPoint publicKey = SM2.importPublicKey("D:/Certificates/publickeyB.pem");
        ECPoint bpublicKey = SM2.importPublicKey("D:/Certificates/publickeyA.pem");
        //最终的请求数据
        JSONObject request = new JSONObject();
        String aesRandomKey = SM2.getAESRandomKey();
        String randomKeyEncrypted = null;
        String parameterJsonEncrypted = null;
        try {
            randomKeyEncrypted = SM2.encrypt(aesRandomKey, bpublicKey);
            parameterJsonEncrypted = SM2.encryptWithBC(data, aesRandomKey);
        } catch (Exception e) {
            System.out.println("加密时，数字签名生成失败");

        }

        request.put("randomKeyEncrypted", randomKeyEncrypted);
        request.put("bizData", parameterJsonEncrypted);

        try {
            Signature signData = SM2.sign(parameterJsonEncrypted, "英大", new SM2KeyPair(publicKey, privateKey));
            request.put("signature", signData);
        } catch (Exception e) {
            System.out.println("加密时，数字签名生成失败");
        }

        System.out.println("参数加密完成" + JSONObject.toJSONString(request));
        return JSONObject.toJSONString(request);
    }


    public static String encryptedS(String data) throws UnsupportedEncodingException {
        SM2 s2 = new SM2();
        BigInteger privateKey = SM2.importPrivateKey("D:/Certificates/privatekeyB.pem");
        ECPoint publicKey = SM2.importPublicKey("D:/Certificates/publickeyB.pem");
        ECPoint bpublicKey = SM2.importPublicKey("D:/Certificates/publickeyA.pem");
        //最终的请求数据
        JSONObject request = new JSONObject();
        String aesRandomKey = SM2.getAESRandomKey();
        String randomKeyEncrypted = null;
        String parameterJsonEncrypted = null;
        try {
            randomKeyEncrypted = SM2.encrypt(aesRandomKey, bpublicKey);
            parameterJsonEncrypted = SM2.encryptWithBC(data, aesRandomKey);
        } catch (Exception e) {
            System.out.println("加密时，数字签名生成失败");

        }

        request.put("randomKeyEncrypted", randomKeyEncrypted);
        request.put("bizData", parameterJsonEncrypted);

        try {
            Signature signData = SM2.sign(parameterJsonEncrypted, "英大", new SM2KeyPair(publicKey, privateKey));
            SignatureS signatureS = new SignatureS(signData.r.toString(), signData.s.toString());
            request.put("signature", signatureS);
        } catch (Exception e) {
            System.out.println("加密时，数字签名生成失败");
        }

        System.out.println("参数加密完成" + JSONObject.toJSONString(request));
        return JSONObject.toJSONString(request);
    }

    public static String decryptedA(String data) throws UnsupportedEncodingException {

        SM2 s2 = new SM2();
        BigInteger privateKey = SM2.importPrivateKey("D:/Certificates/privatekeyA.pem");
        ECPoint publicKey = SM2.importPublicKey("D:/Certificates/publickeyB.pem");
        ECPoint bpublicKey = SM2.importPublicKey("D:/Certificates/publickeyA.pem");
        JSONObject json = JSONObject.parseObject(data);

        //验证签名
        String paramStr = json.getString("bizData");
        Signature signature = json.getObject("signature", Signature.class);
//        try {
//            boolean sign = SM2.verify(paramStr, signature, "英大", bpublicKey);
//            if (sign) {
//                System.out.println("验签成功");
//            } else {
//                System.out.println("验签失败");
//            }
//        } catch (Exception e) {
//            System.out.println("解密时，验签出异常");
//            return null;
//        }

        //处理数据
        String randomKeyEncrypted = json.getString("randomKeyEncrypted");
        String randomKey = SM2.decrypt(randomKeyEncrypted, privateKey);
        String busData = null;
        try {
            busData = SM2.decryptWithBC(paramStr, randomKey);
            System.out.println("解密成功，明文数据为" + busData);
            return busData;
        } catch (Exception e) {
            System.out.println("解密出异常");

        }
        return busData;
    }

    public static String decryptedB(String data) throws UnsupportedEncodingException {

        SM2 s2 = new SM2();
        BigInteger privateKey = SM2.importPrivateKey("D:/Certificates/privatekeyB.pem");
        ECPoint publicKey = SM2.importPublicKey("D:/Certificates/publickeyB.pem");
        ECPoint bpublicKey = SM2.importPublicKey("D:/Certificates/publickeyA.pem");
        JSONObject json = JSONObject.parseObject(data);

        //验证签名
        String paramStr = json.getString("bizData");
        Signature signature = json.getObject("signature", Signature.class);
//        try {
//            boolean sign = SM2.verify(paramStr, signature, "英大", bpublicKey);
//            if (sign) {
//                System.out.println("验签成功");
//            } else {
//                System.out.println("验签失败");
//            }
//        } catch (Exception e) {
//            System.out.println("解密时，验签出异常");
//            return null;
//        }

        //处理数据
        String randomKeyEncrypted = json.getString("randomKeyEncrypted");
        String randomKey = SM2.decrypt(randomKeyEncrypted, privateKey);
        String busData = null;
        try {
            busData = SM2.decryptWithBC(paramStr, randomKey);
            System.out.println("解密成功，明文数据为" + busData);
            return busData;
        } catch (Exception e) {
            System.out.println("解密出异常");

        }
        return busData;
    }

    private static String readTxtFile(String filePath) {
        {
            {
                {
                    InputStreamReader read = null;
                    StringBuffer sb = new StringBuffer();
                    try {
                        String encoding = "gbk";
                        File file = new File(filePath);
                        if (file.isFile() && file.exists()) { //判断文件是否存在
                            //考虑到编码格式
                            read = new InputStreamReader(new FileInputStream(file), encoding);
                            BufferedReader bufferedReader = new BufferedReader(read);
                            String lineTxt = null;
                            while ((lineTxt = bufferedReader.readLine()) != null) {
                                sb.append(lineTxt);
                            }
                        } else {
                            System.out.println("找不到指定的文件");
                        }
                    } catch (Exception e) {
                        System.out.println("读取文件内容出错");
                        e.printStackTrace();
                    } finally {
                        if (read != null) {
                            try {
                                read.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    return sb.toString();

                }
            }
        }
    }


}








